import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { CommonService } from '../../../../services-common/common.service';
import { unionPayMerchantStatusArrs } from '../../cooperation-manage.pipe';
import { UnionPayManageService } from '../unionPay-manage.service';

@Component({
  selector: 'app-cooperation-merchant-list',
  templateUrl: './cooperation-merchant-list.component.html',
  styleUrls: ['./cooperation-merchant-list.component.scss']
})
export class CooperationMerchantListComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '综合搜索', valueKey: 'name', value: '', placeholder: '商户名/商户ID' },
    { type: 1, labelText: '所属拓展员', valueKey: 'userId', value: '', placeholder: 'ID' },
    {
      type: 2,
      labelText: '审核状态',
      valueKey: 'status',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...unionPayMerchantStatusArrs],
    },
    { type: 3, labelText: '申请时间', valueKey: ['applicationBeginTime', 'applicationEndTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  /**
   * 审核 Modal
   * ----------------------------------------------------------------
   */
  isExamineVisible = false;
  examineItem: any = {};
  auditOpinion: string = '';

  constructor(
    private msg: NzMessageService,
    private commonService: CommonService,
    private services: UnionPayManageService
  ) { }

  ngOnInit(): void {
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getMerchantList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      this.tableParams.tableData = data.records || [];
      this.tableParams.total = data.total || 0;
    } else {
      this.msg.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * ----------------------------------------------------------------
   * 审核
   * ----------------------------------------------------------------
   */
  showExamineModal(item: any) {
    this.examineItem = item;
    this.auditOpinion = '';
    this.isExamineVisible = true;
  }

  async subExamine(type: number) {
    if (type === 2 && this.commonService.isEmpty(this.auditOpinion)) {
      this.msg.warning('请输入审核意见');
      return
    }

    let params = {
      id: this.examineItem.id,
      status: type,
      auditOpinion: this.auditOpinion
    }
    const res: any = await this.services.auditMerchantData(params);

    if (res.code === 0) {
      this.isExamineVisible = false;
      this.msg.success('操作成功');
      this.loadTable();
    } else {
      this.msg.error(res.message);
    }
  }
}
